package com.lcx.db137.controller;

import com.lcx.db137.common.R;
import com.lcx.db137.entity.Role;
import com.lcx.db137.service.RolemenuService;
import com.lcx.db137.service.impl.RoleServiceImpl;
import com.lcx.db137.service.impl.RolemenuServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (role)表控制层
 *
 * @author xxxxx
 */
@RestController
@CrossOrigin(origins = "*")
public class RoleController {
    /**
     * 服务对象
     */
    @Autowired
    private RoleServiceImpl roleServiceImpl;
    @Autowired
    private RolemenuServiceImpl rolemenuServiceImpl;

    // 查询所有角色
    @RequestMapping("/getAllRole")
    public R getAllRole() {
        List<Role> list = roleServiceImpl.list();
        return new R().add("roles", list);
    }
    //新增角色
    @PostMapping("/addRole")
    public R addRole(@RequestBody Role role) {
        boolean save = roleServiceImpl.save(role);
        return new R().add("b", save);
    }
    //修改角色
    @PostMapping("/updateRole")
    public R updateRole(@RequestBody Role role) {
        boolean update = roleServiceImpl.updateById(role);
        return new R().add("b", update);
    }
    /**
     * 根据用户id获取拥有的角色集合
     * @param userid
     * @return
     */
    @GetMapping("/selectRoleByUserId/{userid}")
    public R selectRoleByUserId(@PathVariable("userid")int userid){
        List<Role> roles =roleServiceImpl.selectRoleByUserId(userid);
        return new R().add("roles", roles);
    }
}
